package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.*;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Pattern;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

   @Autowired
   private NursingProjectPlanServiceImpl nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //查询护理计划
        NursingPlan nursingPlan = this.getById(id);

        //查询计划关系表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));

        //封装数据
        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(projectplan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(projectplan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(projectplan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(projectplan.getExecuteCycle().toString());
            return nursingProjectPlanVo;

        }).toList();
        vo.setProjectPlans(nursingProjectPlanVos);
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //1.新增计划表
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.save(nursingPlan);
        //2.新增关系表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectplan->{
            projectplan.setPlanId(nursingPlan.getId());
        });
        boolean flag = nursingProjectPlanService.saveBatch(projectPlans);
        return flag ? 1:0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //1.修改计划表
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.updateById(nursingPlan);
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
        if(!CollectionUtil.isEmpty(projectPlans)){
           projectPlans.forEach(projectplan->{
               projectplan.setPlanId(nursingPlan.getId());

            });

        }
        boolean flag = nursingProjectPlanService.saveBatch(projectPlans);
        return flag ? 1:0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return this.removeByIds(Arrays.asList(ids))==true?1:0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)

    {
       this.deleteNursingPlanById( id);
       nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));

        return removeById(id)==true?1:0;
    }
}
